/**
 * @a https://leetcode.cn/problems/spiral-matrix-ii/
 */

#include "../common.h"

class Solution {
public:
    vector<vector<int>> generateMatrix(int n) {
        int nums = n * n;
        int l = 0, r = n - 1, t = 0, b = n - 1;
        vector<vector<int>>ret (n,vector<int>(n));
        int cur = 1;
        while(cur <= nums){
            for(int i = l; i <= r; i++) ret[t][i] = cur++;
            ++t;

            for(int i = t; i <= b; i++) ret[i][r] = cur++;
            --r;

            for(int i = r; i >= l; i--) ret[b][i] = cur++;
            --b;

            for(int i = b; i >= t; i--) ret[i][l] = cur++;
            ++l;
        }
        return ret;
    }
};
